/**
* @file project.h
* @author Jan Sliacky
* 
* Header file for project.c
*
* April 2010
*
* Copyright (c) 2009 by Jan Sliacky
* All rights reserved.
*/

#ifndef PROJECT_H
#define PROJECT_H

#define LEADER 0

#define BC -1

#define CHCKRESULT(func, mess) \
{\
	int ipom; \
	ipom = func; \
	if (ipom != 0) { \
		LOG_ERR(printf("%s [return code: %d]", mess, ipom)); \
		deinitialize(); \
		exit(EXIT_FAILURE); \
	}\
}

#define MUTEX_LOCK(mut) \
{\
	CHCKRESULT(pthread_mutex_lock(mut), "locking mutex failed"); \
	mutexLockUnlockCounter++; \
}

#define MUTEX_INIT(mut) \
{\
	CHCKRESULT(pthread_mutex_init((mut), NULL), "initializing mutex failed"); \
	LOG_TRC(printf("mutex initialized")); \
}

#define MUTEX_DESTROY(mut) \
{\
	CHCKRESULT(pthread_mutex_destroy(mut), "destroying mutex failed"); \
	LOG_TRC(printf("mutex destroyed")); \
}

#define MUTEX_UNLOCK(mut) \
{\
	CHCKRESULT(pthread_mutex_unlock(mut), "unlocking mutex failed"); \
	mutexLockUnlockCounter--; \
}

#define COND_INIT(cond) \
{\
	CHCKRESULT(pthread_cond_init((cond), NULL), "initializing condition failed"); \
	LOG_TRC(printf("condition initialized")); \
}

#define COND_DESTROY(cond) \
{\
	CHCKRESULT(pthread_cond_destroy(cond), "destroying condition failed"); \
	LOG_TRC(printf("condition destroyed")); \
}

#define COND_SIGNAL(cond) \
{\
	CHCKRESULT(pthread_cond_signal(cond), "signaling condition failed"); \
	LOG_TRC(printf("condition signalized")); \
}

#define COND_WAIT(cond, mut, ts) \
{\
	CHCKRESULT(pthread_cond_timedwait((cond), mut, ts), "waiting condition failed"); \
	LOG_TRC(printf("condition wait")); \
}

#define INITTHREAD(name, thread_var, thread) \
{\
	CHCKRESULT(pthread_create(thread_var, NULL, thread, NULL), "thread creating error"); \
	LOG_DBG(printf("%s thread created", name)); \
}

#define JOINTHREADS(name, thread_var) \
{\
	CHCKRESULT(pthread_join(thread_var, NULL), "join thread error"); \
	LOG_DBG(printf("%s thread destroyed", name)); \
}

#define STAT(x) x;

#endif
